package com.hawk.eye.server.service.impl;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hawk.eye.api.storage.dto.LogMessageDTO;
import com.hawk.eye.common.utils.Kv;
import com.hawk.eye.framework.utils.CollectionUtil;
import com.hawk.eye.framework.utils.ExceptionUtils;
import com.hawk.eye.common.page.PageQuery;
import com.hawk.eye.common.page.PageResult;
import com.hawk.eye.framework.utils.SecurityUtils;
import com.hawk.eye.framework.utils.StringUtils;
import com.hawk.eye.server.convert.ServiceLogConvert;
import com.hawk.eye.api.storage.dto.ServiceLogReqDTO;
import com.hawk.eye.server.domain.dto.resp.ServiceLogDTO;
import com.hawk.eye.api.storage.dto.ServiceLogRespDTO;
import com.hawk.eye.server.domain.po.AlarmDetailLog;
import com.hawk.eye.server.mapper.AlarmDetailLogMapper;
import lombok.RequiredArgsConstructor;
import com.hawk.eye.api.storage.ILogMessageStorage;
import org.springframework.stereotype.Component;

/**
 * @author salad
 * @date 2023-10-23
 */
@Component
@RequiredArgsConstructor
public class LogDBMessageStorageImpl implements ILogMessageStorage {

    private final AlarmDetailLogMapper alarmDetailLogMapper;


    @Override
    public String identity() {
        return "MYSQL";
    }

    @Override
    public void saveLogMessage(LogMessageDTO messageDTO, Map<String, Object> alarmLogAsMap) {
        if (CollectionUtil.isEmpty(alarmLogAsMap)) {
            return;
        }
        AlarmDetailLog adl = AlarmDetailLog.builder()
                .baseAlarmId(messageDTO.getBaseAlarmId())
                .chargeUserId(messageDTO.getChargeUserId())
                .serviceName(messageDTO.getServiceName())
                .traceId(messageDTO.getTraceId())
                .alarmTime(messageDTO.getAlarmTime())
                .errorFileName(messageDTO.getErrorFileName())
                .requestUri(messageDTO.getRequestUri())
                .stackHash(messageDTO.getStackHash())
                .logMessage(alarmLogAsMap)
                .build();
        alarmDetailLogMapper.insert(adl);
    }

    @Override
    public PageResult<ServiceLogRespDTO> page(PageQuery pageQuery, ServiceLogReqDTO reqDTO) {
        if (reqDTO.getChargeUserId() != null && reqDTO.getChargeUserId() == -1L) {
            reqDTO.setChargeUserId(SecurityUtils.getUserId());
        }
        IPage<ServiceLogDTO> page = alarmDetailLogMapper.page(pageQuery.toPage(), reqDTO);
        return PageResult.of(page, ServiceLogConvert.INSTANCE::to);
    }

    @Override
    public ServiceLogRespDTO detail(Long baseAlarmId) {
        ExceptionUtils.isEmptyThrow(baseAlarmId, "参数{}不允许为空", "id");
        return ServiceLogConvert.INSTANCE.to(alarmDetailLogMapper.detail(baseAlarmId));
    }

    @Override
    public List<Kv> serviceErrorStatistics(Map<String, Object> params) {
        return alarmDetailLogMapper.serviceErrorStatistics(params);
    }

    @Override
    public List<Kv> errorType(Map<String, Object> params) {
        List<Kv> dataList = alarmDetailLogMapper.errorType(params);
        List<String> colors = StringUtils.getRandomColors(dataList.size());
        for (int i = 0; i < dataList.size(); i++) {
            Kv color = Kv.create();
            color.put("color", colors.get(i));
            dataList.get(i).put("itemStyle", color);
        }
        return dataList;
    }

    @Override
    public List<Kv> serviceErrorTop(Map<String, Object> params) {
        return alarmDetailLogMapper.serviceErrorTop(params);
    }

    @Override
    public List<Kv> problemTop(Map<String, Object> params) {
        return alarmDetailLogMapper.problemTop(params);
    }

}
